package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.DictionaryType;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.service.DTypeService;

@Controller
public class DTypeController {

	@Autowired
	private DTypeService dTypeService;
	
	/**
	 * 批量删除一批数据 或者删除一条记录
	 * @param dTypeIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dType/{dTypeIds}" ,method=RequestMethod.DELETE)
	public Msg deleteUser(@PathVariable(value="dTypeIds")String dTypeIds){

		if(dTypeIds.contains("-")){
			String[] str = dTypeIds.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}

			dTypeService.deleteBatch(ids);
		}else{
			dTypeService.deleteDept(Integer.parseInt(dTypeIds));
		}
		return Msg.success();
	}
	
	/**
	 * 新建字典
	 * @param dType
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dType",method=RequestMethod.PUT)
	public Msg updateDType(DictionaryType dType){
		dTypeService.updateDType(dType);
		return Msg.success();
	}
	
	/**
	 * 新建字典
	 * @param dType
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dType",method=RequestMethod.POST)
	public Msg saveDType(DictionaryType dType){
		dTypeService.insertDType(dType);
		return Msg.success();
	}
	
	@ResponseBody
	@RequestMapping(value="/dType/{id}",method=RequestMethod.GET)
	public Msg getDTypeById(@PathVariable("id")Integer id){
		DictionaryType dType=dTypeService.selectByIdWithModule(id);
		return Msg.success().add("dType", dType);
	}
	
	/**
	 * 获取全部的字典类型  不包括模块
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dTypes",method=RequestMethod.GET)
	public Msg getAllDType(){
		List<DictionaryType> list=dTypeService.selectAll();
		return Msg.success().add("list", list);
	}
	
	/**
	 * 以分页的方式获取字典类型 和其模块
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dTypes/{pn}",method=RequestMethod.GET)
	public Msg getDType(@PathVariable("pn")Integer pn){
		PageHelper.startPage(pn,5);
		List<DictionaryType> list=dTypeService.selectAllWithModule();
		PageInfo<DictionaryType> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
}
